package com.sugon.controller.device;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.DevMaintenancePlanChangeEntity;
import com.sugon.service.DevMaintenancePlanChangeService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备维护计划变更表Controller
 *
 * @author YuXD
 * @date 2020-11-18 15:49
 */
@Controller
@RequestMapping("devmaintenanceplanchange")
public class DevMaintenancePlanChangeController {
    @Autowired
    private DevMaintenancePlanChangeService devMaintenancePlanChangeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("devmaintenanceplanchange:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        // 只有维修开始和结束时间都不为空时才可以调整
        String beginTime = MapUtil.getStr(params, "beginTime");
        String endTime = MapUtil.getStr(params, "endTime");
        if (StrUtil.isNotEmpty(beginTime) && StrUtil.isNotEmpty(endTime)) {
            params.put("beginTime", DateUtil.formatDateTime(DateUtil.beginOfDay(DateUtil.parseDate(beginTime))));
            params.put("endTime", DateUtil.formatDateTime(DateUtil.endOfDay(DateUtil.parseDate(endTime))));
        }else{
            params.remove("beginTime");
            params.remove("endTime");
        }
        //查询列表数据
        Query query = new Query(params);
        List<DevMaintenancePlanChangeEntity> devMaintenancePlanChangeList = devMaintenancePlanChangeService.queryList(query);
        int total = devMaintenancePlanChangeService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(devMaintenancePlanChangeList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("devmaintenanceplanchange:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevMaintenancePlanChangeEntity devMaintenancePlanChange = devMaintenancePlanChangeService.queryObject(id);

        return R.ok().put("devMaintenancePlanChange", devMaintenancePlanChange);
    }

    /**
     * 保存
     */
    @AutoLog("设备维护计划变更表--保存")
    @RequestMapping("/save")
    @RequiresPermissions("devmaintenanceplanchange:save")
    @ResponseBody
    public R save(@RequestBody DevMaintenancePlanChangeEntity devMaintenancePlanChange) {
        devMaintenancePlanChangeService.save(devMaintenancePlanChange);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("设备维护计划变更表--修改")
    @RequestMapping("/update")
    @RequiresPermissions("devmaintenanceplanchange:update")
    @ResponseBody
    public R update(@RequestBody DevMaintenancePlanChangeEntity devMaintenancePlanChange) {
        devMaintenancePlanChangeService.update(devMaintenancePlanChange);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("设备维护计划变更表--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("devmaintenanceplanchange:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devMaintenancePlanChangeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevMaintenancePlanChangeEntity> list = devMaintenancePlanChangeService.queryList(params);

        return R.ok().put("list", list);
    }
}
